// 证券管理表格页面
import React from 'react';
import { connect } from 'dva';
import { stringify } from 'qs';
import { PrimaryC, TableC, PrimaryGhostC } from '../../../../comps';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Modal } from 'antd';
import AdiustC from './adjustC';
import AddNew from './addNewC';
import TransferC from './transferC';
import { isZiGuanEvir, exportFile } from '../../../../utils/commonFunc';
import { findValueByCode } from '../../../../utils/utils';
@Form.create()
class securityManage extends React.Component {
  state = {
    adiustVisible: false, // 调整证券模态框
    addVisible: false, // 新增模态框
    transferVisible: false, // 划转证券模态框
    buttonOk: true, // 调整证券是否可以点击
    tableData: {}, // 传入模态框数据
    value: '', // 证券调整按钮
    key: -1,
    rowData: '',
    operateType: [
      { key: 0, value: '证券增加' },
      { key: 1, value: '证券减少' },
      // { key: 2, value: "质押券增加" },
      // { key: 3, value: "质押券减少" },
      // { key: 4, value: "证券冻结" },
      // { key: 5, value: "证券解冻" },
    ],
  };

  componentDidMount() {
    this.setState({
      buttonOk: true,
    });
    this.props.onRef(this);
    if (this.props.flag) {
      //证券冻结、证券解冻
      this.state.operateType.push({ key: 4, value: '证券冻结' }, { key: 5, value: '证券解冻' });
    }
  }

  // 改变模态框
  changeVisible = (msg) => {
    this.setState({
      adiustVisible: false,
      addVisible: false,
      transferVisible: false,
    });
  };

  // 表格页码排序改变调用
  handleTableChange = (pagination) => {
    this.props.handleTableChange(pagination);
    this.setState({
      buttonOk: true,
      rowData: '',
    });
  };

  // 表格数据点击响应
  tableChange = (record) => {
    if (record) {
      let id = record.combiId;
      this.props.dispatch({
        type: 'positionManageM/getJurisdiction',
        payload: { id },
        callback: (data) => {
          if (data && data.dataSource.operationPermission) {
            this.setState({
              buttonOk: false,
              tableData: record,
            });
          }
        },
      });
    }
  };

  // 调整证券响应
  adjustClick = (e) => {
    this.state.operateType.map((item) => {
      if (item.key == e) {
        this.setState({ value: item.value });
      }
    });
    this.setState({
      adiustVisible: true,
      key: e,
    });
  };

  // 划转证券点击响应
  transferClick = () => {
    this.setState({ transferVisible: true });
  };

  // 新增持仓响应
  AddNewClick = () => {
    this.setState({ addVisible: true });
  };

  export = () => {
    const { form, queryItems } = this.props;
    const values = {
      dataDt: queryItems.dataDt,
      fundId: queryItems.fundId?.join(),
      exchangecodes: queryItems.exchangeCode,
      tradingcodes: queryItems.tradingCode,
      securityType: queryItems.securityType,
      investmentType: queryItems.investmentType,
      userId: localStorage.getItem('antd-pro-userId'),
    };
    exportFile('api/v1/holdInfo/exportfind2', values);
  };
  onRef = () => {
    this.setState({
      buttonOk: true,
      rowData: '',
    });
  };
  render() {
    const {
      columns,
      dataSource,
      total,
      currentPage,
      pageSize,
      flag,
      globalM: {
        dataDictionary: { ENTRUSTED_INVESTMENT_HOLD_ATTRCODE = [] },
      },
    } = this.props;
    const { buttonOk, adiustVisible, tableData, value, key, addVisible, transferVisible, rowData } = this.state;
    columns.map((item) => {
      if (item.key == 'shortBlankSign') {
        item.render = (text) => {
          return findValueByCode(text, ENTRUSTED_INVESTMENT_HOLD_ATTRCODE);
        };
      }

      // 日期
      if (item.key == 'id.dataDt') {
        item.render = (_, record) => {
          const text = record?.id?.dataDt;
          if (text != '--' && text) {
            return text.slice(0, 4) + '-' + text.slice(4, 6) + '-' + text.slice(6);
          } else {
            return '--';
          }
        };
      }
      // 证券代码
      if (item.key == 'id.tradingcode') {
        item.render = (_, record) => {
          const text = record?.id?.tradingcode;
          if (text != '--' && text) {
            return text;
          } else {
            return '--';
          }
        };
      }
    });
    return (
      <div>
        <div style={{ marginTop: '10px' }}>
          {this.state.operateType.map((item, key) => (
            <PrimaryC title={item.value} style={{ margin: '5px' }} disabled={buttonOk} onClick={() => this.adjustClick(item.key)} key={key} />
          ))}
          <PrimaryC title={'划转证券'} style={{ margin: '5px' }} onClick={() => this.transferClick()} />
          {flag && <PrimaryC title={'新增持仓'} style={{ margin: '5px' }} onClick={() => this.AddNewClick()} />}
          {/* <PrimaryGhostC
                        title="导出"
                        style={{ margin: '5px', float: 'right' }}
                        onClick={this.export}
                    /> */}
        </div>
        <div style={{ marginTop: '10px' }}>
          <TableC
            columns={columns}
            loading={this.props.loading}
            dataSource={dataSource}
            current={currentPage}
            pageSize={pageSize}
            total={total}
            selfScrollY={true}
            scroll={{ x: 'max-content' }}
            rowKey='key'
            pagination={true}
            onChange={this.handleTableChange}
            onRow={(record, index) => {
              return {
                onClick: () => {
                  this.tableChange(record);
                  this.setState({
                    rowData: index,
                  });
                },
              };
            }}
            rowClassName={(record, index) => {
              return index === rowData ? 'trade-select' : '';
            }}
          />
        </div>
        {adiustVisible && <AdiustC visible={adiustVisible} tableData={tableData} value={value} type={key} onRefesh={this.props.query} changeVisible={this.changeVisible} />}
        {addVisible && <AddNew visible={addVisible} onRefesh={this.props.query} changeVisible={this.changeVisible} />}
        {transferVisible && <TransferC visible={transferVisible} onRefesh={this.props.query} changeVisible={this.changeVisible} />}
      </div>
    );
  }
}

export default connect(({ positionManage, globalM, loading, positionInquiryM }) => ({
  positionManage,
  positionInquiryM,
  globalM,
  loading: loading.effects['positionManageM/getManageList'],
}))(securityManage);
